<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$return=array();
$return['basic']=$this->plugin_setting('basic');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
set_html('title','订单');
if($_RQ['op']=='main'){
	$return['params']=array('status'=>$_RQ['status']?intval($_RQ['status']):100);
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//超时未支付订单取消
	$list=pdo_getall('jy_shopbaoxiu_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>'0'));
	if($list){
		$timeout=$return['basic']['timeout']?$return['basic']['timeout']:5;
		foreach($list as $row){
			if(time()-$row['createtime']>$timeout*60){
				pdo_update('jy_shopbaoxiu_order',array('status'=>3),array('id'=>$row['id']));
			}
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	//查询进入该页面的客户所有订单
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status'] != 100){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	$order_list=pdo_getslice('jy_shopbaoxiu_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($order_list){
		$jy_wxshifu=$this->plugin_exists('jy_wxshifu');
		foreach($order_list as &$item){
			$serve=pdo_get('jy_shopbaoxiu_list',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']));
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['serve_time']=date('Y-m-d H:i',$item['serve_time']);
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$item['typetitle']=$this->order_types[$item['type']];
			$item['statustitle']=$this->fuwuorder_status[$item['status']];
			if($item['status']==0){
				$item['statusdesc']='超时未支付，订单将自动取消';
			}elseif($item['status']==1){
				$item['statusdesc']='请您留意订单进度';
			}elseif($item['status']==2){
				$item['statusdesc']='欢迎下次光临!';
			}elseif($item['status']==3){
				$item['statusdesc']='欢迎下次光临！';
			}elseif($item['status']==4){
				$item['statusdesc']='欢迎下次光临！';
			}elseif($item['status']==5){
				$item['statusdesc']='请您留意退款进度';
			}elseif($item['status']==6){
				$item['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
			}else{
				$item['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
			}
			$item['store']=pdo_get('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title','pcd','address'));
			$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('city'=>'');
			if($jy_wxshifu){
				$item['shifuinfo']=model('jy_wxshifu/model')->getshifu($item,'jy_shopbaoxiu');
				$item['shifuinfo']['mobile']=$item['shifuinfo']['mobile']?cfc('tools')->substr($item['shifuinfo']['mobile'],0,7).'****':'无电话';
			}
			$item['serve_image']=tomedia($item['serve_image']);
		}
	}
	$return['list']=$order_list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_shopbaoxiu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order['package']=json_decode($order['package']);
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	//项目信息
	$serve=pdo_get('jy_shopbaoxiu_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pid']));	
	$order['serve_time']=date('Y-m-d H:i',$order['serve_time']);
	$order['typetitle']=$this->order_types[$order['type']];
	$order['serve_image']=$serve['image'];
	$order['images']=$order['images']?json_decode($order['images'],true):array();
	//自定义字段数据
	$fileds=$order['fileds']?json_decode($order['fileds'],true):array();
	foreach($fileds as &$item){
	    if($item['data'] && $item['type']==5){
			$item['data']=rtrim($item['data'], ";");
			$item['data']=ltrim($item['data'], ";");
		}
	}
	if($order['selfnumber']){
		$order['selfnumberurl']=custom_url('jy_shopbaoxiu/order.selfdetail//selfnumber='.$order['selfnumber'],true);
		//小程序二维码：
		$url='/jy_shopbaoxiu/order/selfdetail/index?selfnumber='.$order['selfnumber'];
		$pid='verifyid'.$order['id'].'c'.$order['selfnumber'];
		$order['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
	}
	$order['statustitle']=$this->fuwuorder_status[$order['status']];
	//定义订单状态名称
	if($order['status']==0){
		$order['statusdesc']='超时未支付，订单将自动取消';
	}elseif($order['status']==1){
		$order['statusdesc']='请您留意订单进度';
	}elseif($order['status']==2){
		$order['statusdesc']='欢迎下次光临!';
	}elseif($order['status']==3){
		$order['statusdesc']='欢迎下次光临！';
	}elseif($order['status']==4){
		$order['statusdesc']='欢迎下次光临！';
	}elseif($order['status']==5){
		$order['statusdesc']='请您留意退款进度';
	}elseif($order['status']==6){
		$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
	}else{
		$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
	}
	$order['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$order['id'],'plugin'=>'jy_shopbaoxiu'),'pay_type');
	$order['paytype']=get_pay_types()[$order['paytype']];
	$jy_wxshifu=$this->plugin_exists('jy_wxshifu');
	if($jy_wxshifu){
		$order['shifuinfo']=model('jy_wxshifu/model')->getshifu($order,'jy_shopbaoxiu');
		$order['shifuinfo']['mobile']=$order['shifuinfo']['mobile']?cfc('tools')->substr($order['shifuinfo']['mobile'],0,7).'****':'无电话';
	}
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$serve;
	$return['worker']=$worker;
	$return['fileds']=$fileds;
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('model')->order_receive($_RQ['id']);
	exi('确认完成服务成功！');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_shopbaoxiu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_shopbaoxiu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='paynew'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_shopbaoxiu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$params=array(
					'id'=>$id,
					'title'=>'维修订单',
					'fee'=>$order['payfee'],
					'plugin'=>'jy_shopbaoxiu',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_shopbaoxiu/fuwu.order/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_shopbaoxiu/fuwu.order/',
					'error_url'=>'jy_shopbaoxiu/fuwu.order/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	$this->pdo_update('order',array('pay_id'=>$payid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi($payid);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	$order=pdo_get('jy_shopbaoxiu_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	$this->model('model')->payresult4($order,$payresult);
	exi('支付成功','success','jy_shopbaoxiu/fuwu.order/list/status=1');
}